package com.thingcom.delinrobot.main.Launch.NewUser;

import android.annotation.SuppressLint;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import com.thingcom.delinrobot.Common.Constant;
import com.thingcom.delinrobot.R;
import com.thingcom.delinrobot.base.BaseBackFragment;
import com.thingcom.delinrobot.utils.AppUtils;
import com.thingcom.delinrobot.utils.PreferencesUtil;
import com.thingcom.delinrobot.view.SimpleToolbar;
import java.util.Objects;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import static com.thingcom.delinrobot.utils.AppUtils.hideSoftKeyboard;

/**
 * author : YuFeng
 * e-mail : 923462992@qq.com
 * date   : 2019/11/27
 */
public class RegisterMailFragment extends BaseBackFragment {


    @BindView(R.id.toolbar_login_mail)
    SimpleToolbar toolbarLoginMail;
    @BindView(R.id.btn_login_mail_continue)
    Button btnLoginMailContinue;
    @BindView(R.id.et_login_mail)
    TextInputEditText etLoginMail;
    @BindView(R.id.til_login_mail)
    TextInputLayout tilLoginMail;
    @BindView(R.id.cl_register_mail)
    ConstraintLayout clRegisterMail;

    public static RegisterMailFragment newInstance() {
        Bundle args = new Bundle();
        RegisterMailFragment fragment = new RegisterMailFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_back_register_mail, container, false);
        unbinder = ButterKnife.bind(this, view);
        initToolbar(toolbarLoginMail);
        btnLoginMailContinue.setEnabled(false);
        btnLoginMailContinue.setBackgroundColor(getResources().getColor(R.color.grey_button));

//        hint字体的大小等属性
//        tilLoginMail.setCounterTextAppearance(R.style.hintAppearance);
        ColorStateList colorStateList = getResources().getColorStateList(R.color.text_input_edit_selectoe);
        tilLoginMail.setBoxStrokeColorStateList(colorStateList);
        clRegisterMail.setOnTouchListener((v, event) -> {
            clRegisterMail.clearFocus();
            hideSoftKeyboard(getContext(),clRegisterMail);
            return false;
        });
        etLoginMail.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                //这个方法被调用，说明在s字符串中，从start位置开始的count个字符即将被长度为after的新文本所取代。
                // 在这个方法里面改变s，会报错。
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //这个方法被调用，说明在s字符串中，从start位置开始的count个字符刚刚取代了长度为before的旧文本。
                // 在这个方法里面改变s，会报错。

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (AppUtils.isEmail(s.toString())){
                    btnLoginMailContinue.setEnabled(true);
                    btnLoginMailContinue.setBackground(getResources().getDrawable(R.drawable.selector_button_main));
                } else {
                    btnLoginMailContinue.setEnabled(false);
                    btnLoginMailContinue.setBackgroundColor(getResources().getColor(R.color.grey_button));
                }
                //这个方法被调用，那么说明s字符串的某个地方已经被改变。
            }
        });
        return view;
    }

    @OnClick(R.id.btn_login_mail_continue)
    public void onViewClicked(View view) {
        if (view.getId() == R.id.btn_login_mail_continue) {
            PreferencesUtil.getInstance(getContext()).putString(Constant.MAIL_NUMBER, Objects.requireNonNull(etLoginMail.getText()).toString());
            hideSoftKeyboard(_mActivity);
            start(RegisterNameFragment.newInstance());
        }
    }
}
